<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>新增借用单</title>
    <link rel="stylesheet" href="/lib/layui/layui/css/layui.css">
    <link rel="stylesheet" href="/lib/layui/layui/css/common.css">
    <script src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="../../js/jquery/jquery-1.9.1.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/lib/layui/layui/global.js"></script>
    <script src="/lib/layui/layui/js/common.js?v=20190726" type="text/javascript" charset="utf-8"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .layui-card-header{
            border-bottom: 1px solid #eee;
        }
        .newTable{
            width: 80%;
            margin: 0 auto;
        }
        .layui-form .one{
            width: 100%;
            display: flex;
            flex-flow: row wrap;
            justify-content: space-around;
        }
        .layui-form-item{
            width: 48%;
        }
        .layui-inline{
            width: 40%;
            height: 38px;
        }
        .layui-form-label{
            width:64px;
        }
        .layui-input-block{
            margin-left: 140px;
            width: 240px;
        }
        select{
            width: 100%;
            height: 38px;
        }
        input::-webkit-input-placeholder {
            color: #aab2bd;
        }
        .buttonbottom{
            text-align: center;
        }
        .buttonbottom button{
            background-color: #009284;
            width: 90px;
            margin-top: 30px;
        }
        .layui-treeSelect .ztree li span.button.root_open:before, .layui-treeSelect .ztree li span.button.root_close:before{
            content: "\e623";
        }
        .layui-form-label{
            width: 100px;
        }
        .layui-table{
            width:100%!important;
        }
        .layui-disabled, .layui-disabled:hover{
            color:#666!important;
        }
    </style>
</head>
<body>
<div class="mbox">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="formTest" action=""style="width: 100%;margin: 0 auto;">
                <div style="width: 100%;height:36px;line-height:36px;font-weight:bold;padding-left: 6px;background-color: #eeeeee">转移信息</div>
                <div class="one" style="padding-top: 20px;">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;"><span style="color: red;">*</span>原部门</label>
                        <div class="layui-input-block" style="width: 240px;" id="deptoper">
                            <select name="originalDeptId" style="" id="originalDeptId" lay-filter="selectdept1" >
                                <option></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;"><span style="color: red;">*</span>原资产管理员</label>
                        <div class="layui-input-block" style="width: 240px;">
                            <input type="text" id="originalCustodian" name="originalCustodian" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="one">
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label"><span style="color: red;">*</span>新部门</label>
                        <div class="layui-input-block" id="newDept">
                            <select name="newDeptId" style="" id="newDeptId" lay-filter="selectdept2" >
                                <option></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label"><span style="color: red;">*</span>新资产管理员</label>
                        <div class="layui-input-block">
                            <input type="text" name="newCustodian" id="newCustodian" placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="one">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>调拨日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="transferDate" id="transferDate" lay-verify="required" autocomplete="off" class="layui-input"  placeholder="yyyy-MM-dd" >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="memo"  id="memo"  placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="one">
                    <div class="layui-form-item">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" name="application"  id="application"  placeholder="请输入" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red;">*</span>申请时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="applicationTime" id="applicationTime" lay-verify="required" autocomplete="off" class="layui-input"  placeholder="yyyy-MM-dd" >
                        </div>
                    </div>
                </div>
                <div id="eqba" style="width: 100%;height:36px;line-height:36px;font-weight:bold;padding-left: 6px;background-color: #eeeeee">设备信息</div>
                <table id="Settlement" lay-filter="SettlementFilter"></table>
            </form>
        </div>
    </div>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="add" id="add" style="width: 70px">选择设备</button>
        <button type="button" class="layui-btn layui-btn-sm" lay-event="del" style="width: 70px">删除</button>
    </div>
</script>
<script>
    // 获取地址栏参数值
    function getQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    var begin = getQueryString("begin");
    var ownerlogId = getQueryString("ownerlogId");
    var SettlementTable;
    var eqdata;
    var userName;
    var deptId=0;
    layui.use(['form','table', 'laydate','element'], function() {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate
            , table = layui.table
            , $ = layui.jquery;

        if(begin == "add"){
            // 申请时间默认当前
            $("#applicationTime").val(nowformat);
            //原部门所属组织下拉框
            $.ajax({
                type: 'get',
                url: '/equipTransfer/userDept',
                dataType: 'json',
                success: function (data) {
                    var render = [];
                    var deptdata = data.data;
                    var userName = data.object.userName;
                    var userId = data.object.userId;
                    for (var i = 0; i < deptdata.length; i++) {
                        render.push("<option class='' value=\"" + deptdata[i].deptId + "\">" + deptdata[i].deptName + "</option>");
                    }
                    $("#originalDeptId").html(render.join(""));
                    form.render();
                    $("#application").val(userName);
                    $("#application").attr("data-id",userId);
                    $("#application").attr("disabled",true);
                }
            });

            //新部门所属组织下拉框
            $.ajax({
                type: 'get',
                url: '/equipTransfer/findRecDept',
                dataType: 'json',
                success: function (data) {
                    debugger
                    var render = [];
                    var ndeptdata = data.data;
                    render.push("<option></option>");
                    for (var i = 0; i < ndeptdata.length; i++) {
                        if(ndeptdata[i].deptId!=undefined){
                            render.push("<option class='' value=\"" + ndeptdata[i].deptId + "\">" + ndeptdata[i].deptName + "</option>");
                        }
                    }
                    $("#newDeptId").html(render.join(""));
                    form.render();
                }
            });
            // 表格实例
            SettlementTable = table.render({
                elem: '#Settlement'
                , data: []
                , toolbar: '#toolbar'
                ,cols: [[ //表头
                     {type: 'checkbox',LAY_CHECKED:true }
                    ,{field: 'equipNo', title: '固定资产编码'}
                    ,{field: 'equipName', title: '设备名称'}
                    ,{field: 'equipTypeName', title: '设备类型'}
                    ,{field: 'deptName', title: '所属部门'}
                    ,{field: 'equipStatuasName', title: '资产状态'}
                    ,{field: 'modelNo', title: '型号'}
                    ,{field: 'spec', title: '规格'}
                    ,{field: 'positionName', title: '原存放位置'}
                ]]
                , done: function (res) {
                    if(res.data.length==0){
                        $('.layui-form-checkbox').removeClass('layui-form-checked')
                    }
                }
            });
            // 表格头事件
            table.on('toolbar(SettlementFilter)', function (obj)  {
                var layEvent = obj.event;
                var deptid = $("#deptoper").find("dl dd.layui-this").attr("lay-value");
                switch (layEvent) {
                    case 'add':
                        var odeid = $("#deptoper").find("dl dd.layui-this").attr("lay-value");
                        var ndeid = $("#newDept").find("dl dd.layui-this").attr("lay-value");
                        if(odeid == ndeid){
                            layer.msg("同部门设备不需要转移。");
                            return false;
                        }else{
                            layer.open({
                                type: 2,
                                skin: false, //加上边框
                                area: [ '960px','98%'], //宽高
                                title: '选择设备',
                                btn:['确定','取消'],
                                btnAlign:'c',
                                content: '/equipTransfer/selectEquip?deptId='+ deptid
                                ,success: function (res) {

                                },
                                yes:function (index,layero) {
                                    eqdata = $(layero).find("iframe")[0].contentWindow.getData();
                                    var aarr = layui.table.cache["Settlement"];
                                    if(eqdata.length == 0){
                                        layer.msg("请选择设备添加到下方表格并选中添加的设备再点击确定！");
                                        return false;
                                    }else{
                                        layer.close(index);
                                        for(var i=0;i<eqdata.length;i++){
                                            aarr.push(eqdata[i]);
                                        }
                                        // 数组对象去重
                                        var result = [];
                                        var obj = {};
                                        for(var i =0; i<aarr.length; i++){
                                            if(!obj[aarr[i].equipId]){
                                                result.push(aarr[i]);
                                                obj[aarr[i].equipId] = true;
                                            }
                                        }
                                        SettlementTable.reload({
                                            data:result
                                        })
                                    }
                                }
                            });
                        }
                        break;
                    case 'del':
                        var ar = layui.table.cache["Settlement"];
                        var checkStatus = table.checkStatus('Settlement').data;
                        for(var i=0;i<checkStatus.length;i++){
                            deleteItem(checkStatus[i].equipId, ar);
                        }
                        if(checkStatus==undefined||checkStatus.length<=0){
                            layer.msg("请选中一条或者多条数据进行删除");
                        }
                        if (checkStatus != undefined && checkStatus.length > 0) {
                            SettlementTable.reload({
                                data:ar
                            })
                        }
                        break;
                }
            });
        }
        if(begin == "edit"){
            $("#eqba").css("display","none");
            $.ajax({
                type: "post",
                url: "/equipTransfer/editApprovalInfo",
                dataType: "json",
                data: {
                    ownerlogId:ownerlogId
                },
                success: function (res) {
                    var objdata = res.object;
                    form.val("formTest",objdata);
                    //原部门所属组织下拉框
                    $.ajax({
                        type: 'get',
                        url: '/equipTransfer/userDept',
                        dataType: 'json',
                        success: function (data) {
                            var render = [];
                            var deptdata = data.data;
                            var userName = data.object.userName;
                            var userId = data.object.userId;
                            for (var i = 0; i < deptdata.length; i++) {
                                render.push("<option class='' value=\"" + deptdata[i].deptId + "\">" + deptdata[i].deptName + "</option>");
                            }
                            $("#originalDeptId").html(render.join(""));
                            $('#originalDeptId').find('option[value="'+objdata.originalDeptId+'"]').prop('selected',true);
                            form.render();
                            $("#application").val(userName);
                            $("#application").attr("data-id",userId);
                            $("#application").attr("disabled",true);
                        }
                    });
                    //新部门所属组织下拉框
                    $.ajax({
                        type: 'get',
                        url: '/equipTransfer/findRecDept',
                        dataType: 'json',
                        success: function (data) {
                            var render = [];
                            var ndeptdata = data.data;
                            render.push("<option></option>");
                            for (var i = 0; i < ndeptdata.length; i++) {
                                render.push("<option class='' value=\"" + ndeptdata[i].deptId + "\">" + ndeptdata[i].deptName + "</option>");
                            }
                            $("#newDeptId").html(render.join(""));
                            $('#newDeptId').find('option[value="'+objdata.newDeptId+'"]').prop('selected',true);
                            form.render();
                        }
                    });
                    $("#application").val(objdata.applicationName);
                    $("#application").attr("data-id",objdata.application);
                    $("#newCustodian").val(objdata.newCustodianName);
                    $("#newCustodian").attr("data-id",objdata.newCustodian);
                    $('#originalDeptId').attr("disabled",true);
                }
            })
        }
        // 日期选择--委托日期
        laydate.render({
            elem:'#transferDate'
            ,trigger: 'click'//呼出事件改成click
            ,format: 'yyyy-MM-dd'
        });
        // 日期选择--应出报告日期
        laydate.render({
            elem:'#applicationTime'
            ,trigger: 'click'//呼出事件改成click
            ,format: 'yyyy-MM-dd'
        });
        $("#newCustodian").click(function () {
            if (deptId == 0||deptId == ''||deptId == undefined||deptId==null) {
                layui.layer.msg("请选择借用部门")
                return false;
            }
            borrow = "borrow";
            $.popWindow("../common/lmisSelectUserExp?0");
        })
        form.on('select(selectdept2)', function (data) {

            deptId = data.elem.value;
            console.log(deptId)
        });

    });
    var borrow;
    // 数组对象的删除
    function deleteItem (item, list) {
        // 先遍历list里面的每一个元素，对比item与每个元素的id是否相等，再利用splice的方法删除
        for (var key in list) {
            if (list[key].equipId === item) {
                list.splice(key, 1)
            }
        }
    };
    //获取当前时间  年月日
    function nowformat() {
        var nstr = "";
        var now = new Date();
        var nyear = now.getFullYear();
        var nmonth = now.getMonth() + 1;
        var nday = now.getDate();
        if (nmonth < 10) {
            nmonth = "0" + nmonth;
        }
        if (nday < 10) {
            nday = "0" + nday;
        }
        nstr = nyear + "-" + nmonth + "-" + nday;
        return nstr;
    }
    function addData() {
        var ownerLog = {};
        ownerLog.originalDeptId = $("#deptoper").find("dl dd.layui-this").attr("lay-value");
        ownerLog.newDeptId = $("#newDept").find("dl dd.layui-this").attr("lay-value");
        ownerLog.newCustodian = $("#newCustodian").attr("data-id");
        ownerLog.originalCustodian = $("#originalCustodian").val();
        ownerLog.application = $("#application").attr("data-id");
        ownerLog.memo = $("#memo").val();
        ownerLog.applicationTime = $("#applicationTime").val();
        ownerLog.transferDate = $("#transferDate").val();
        return ownerLog;
    }
    function addjData() {
        var jsondata = layui.table.checkStatus('Settlement').data;
        return jsondata;
    }
    function Xrtransfer(res){
        var newuserId = res.userId;
        var newuserName = res.userName;
        $("#newCustodian").val(newuserName);
        $("#newCustodian").attr("data-id",newuserId);
    }
</script>
</body>
</html>
